/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.apilist;

/**
 * RequestResultException 是封装异常返回值的快捷方式
 * <p>在本架构中，门面web或者微服务会对RequestResultException异常进行封装
 * <p>微服务层的RequestResultException会通过{@link RestfulBaseController}被转换成{@link RequestResult}
 * <p>门面web的RequestResultException会被转换成JsonReturnCodeEnum统一格式返回前端
 *
 * @see RequestResult
 * @see JsonReturnCode
 * @author yoara
 */
public class RequestResultException extends RuntimeException {
    private String code;
    private String msgBody;
    private JsonReturnCode codeEnum;

    public RequestResultException(String message) {
        this(message,null,null);
    }

    protected RequestResultException(String message,String code) {
        this(message,code,null);
    }

    public RequestResultException(JsonReturnCode codeEnum) {
        this(codeEnum.getMessage(),codeEnum.getStatus(),codeEnum);
    }

    public RequestResultException(String message, JsonReturnCode codeEnum) {
        this(message,codeEnum.getStatus(),codeEnum);
    }

    private RequestResultException(String message, String code, JsonReturnCode codeEnum) {
        super(message);
        this.code = code;
        this.codeEnum = codeEnum;
    }

    public String getCode() {
        return code;
    }

    public JsonReturnCode getCodeEnum() {
        return codeEnum;
    }

    public String getMsgBody() {
        return msgBody;
    }

    public RequestResultException setMsgBody(String msgBody) {
        this.msgBody = msgBody;
        return this;
    }

    public RequestResultException setCode(String code) {
        this.code = code;
        return this;
    }

    public RequestResultException setCodeEnum(JsonReturnCode codeEnum) {
        this.codeEnum = codeEnum;
        return this;
    }
}